//
//  LHRadioListController.m
//  EPA
//
//  Created by 卢浩 on 16/4/21.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHRadioListController.h"
#import "LHNetWorkRequestManager.h"
#import "LHRadioListTableViewCell.h"
#import "LHRadioListModel.h"
#import "LHRadioDetailModel.h"
#import "LHRadioDetailViewController.h"
#import "LORefresh.h"

@interface LHRadioListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *radioListData;

@property (nonatomic,strong) UIImageView *imageView;

@property (nonatomic,assign) NSInteger start;

@end

@implementation LHRadioListController

- (void)dealloc
{
    //自定义图片
    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];
    
    //底部
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.titleStr;
    
    self.radioListData = [NSMutableArray array];
    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width * 0.6)];
    self.imageView.image = [UIImage imageNamed:@"图片正在加载.jpeg"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"LHRadioListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"LHRadioListTableViewCell"];
    
    [self loadDataWithRadioID:self.radioid];
    
    __weak typeof(self) weakSelf = self;
    
    //自定义图片
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        [weakSelf.radioListData removeAllObjects];
        [weakSelf.tableView reloadData];
        weakSelf.start = 0;
        [weakSelf loadDataWithRadioID:weakSelf.radioid];
    }];
    [[self.tableView gifHeader] setGifName:@"wangnima"];
    
    //添加上提加载更多
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.radioListData.count;
        [weakSelf loadDataWithRadioID:weakSelf.radioid Start:count];
    }];
}

- (void)loadDataWithRadioID:(NSString *)radioID{
    NSDictionary *paramsDict = @{@"auth":[LHUserInfoManager shareManager].auth,@"client":@1,@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"radioid":radioID,@"version":@"3.0.4"};
    [LHNetWorkRequestManager requestUrl:[NSURL URLWithString:RADIODETAILLIST_URL] Methed:POST Params:paramsDict Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
//            NSLog(@"%@",dict);
            NSDictionary *dataDict = dict[@"data"];
//            NSLog(@"%@",dataDict[@"radioInfo"][@"coverimg"]);
            NSString *str = dataDict[@"radioInfo"][@"coverimg"];
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:str]];
            NSArray *listArr = dataDict[@"list"];
            NSMutableArray *tempListArr = [NSMutableArray array];
            for (NSDictionary *radioDict in listArr) {
                LHRadioListModel *model = [[LHRadioListModel alloc]init];
                [model setValuesForKeysWithDictionary:radioDict];
                [tempListArr addObject:model];
//                NSLog(@"%@",model.musicUrl);
//                NSLog(@"%@",model.radioDetailModel.webview_url);
//                NSLog(@"%@",model.radioDetailModel.authorinfo);
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.radioListData addObjectsFromArray:tempListArr];
                [self.tableView reloadData];
                //自定义图片
                [[self.tableView gifHeader] endRefreshing];
            });
        }
    }];
}

- (void)loadDataWithRadioID:(NSString *)radioID Start:(NSInteger)start{
    NSDictionary *paramsDict = @{@"auth":[LHUserInfoManager shareManager].auth,@"client":@(1),@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"radioid":radioID,@"start":@(start),@"version":@"3.0.4"};
    [LHNetWorkRequestManager requestUrl:[NSURL URLWithString:RADIODETAILMORE_URL] Methed:POST Params:paramsDict Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dataDict = dict[@"data"];
            NSArray *listArr = dataDict[@"list"];
            NSMutableArray *tempListArr = [NSMutableArray array];
            for (NSDictionary *radioDict in listArr) {
                LHRadioListModel *model = [[LHRadioListModel alloc]init];
                [model setValuesForKeysWithDictionary:radioDict];
                [tempListArr addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.radioListData addObjectsFromArray:tempListArr];
                [self.tableView reloadData];
                //底部
                [[self.tableView defaultFooter] endRefreshing];
            });
        }
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return self.radioListData.count;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return self.view.frame.size.width * 0.6 + 2;
    }else{
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc]init];
        [view addSubview:self.imageView];
        return view;
    }else{
        return nil;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LHRadioListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHRadioListTableViewCell" forIndexPath:indexPath];
    cell.model = self.radioListData[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LHRadioDetailViewController *radioDetailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"LHRadioDetailViewController"];
    radioDetailVC.radioArray = self.radioListData;
    radioDetailVC.index = indexPath.row;
    radioDetailVC.radioid = self.radioid;
    [self.navigationController pushViewController:radioDetailVC animated:YES];
}

@end
